# 2. text_render.py

import sdl2
from PIL import Image, ImageDraw, ImageFont

def render_text_image(text):
    img = Image.new("RGBA", (720, 720), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 48)
    draw.text((50, 650), text, font=font, fill=(255, 255, 255, 255))
    return img

def render_viseme_line(viseme):
    img = Image.new("RGBA", (720, 80), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 36)
    draw.text((10, 10), viseme, font=font, fill=(255, 255, 0, 255))
    return img

def pil_image_to_texture(image, renderer):
    raw_bytes = image.tobytes("raw", "RGBA")
    surface = sdl2.SDL_CreateRGBSurfaceFrom(
        raw_bytes, image.width, image.height, 32, image.width * 4,
        0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
    )
    texture = sdl2.SDL_CreateTextureFromSurface(renderer, surface)
    sdl2.SDL_SetTextureBlendMode(texture, sdl2.SDL_BLENDMODE_BLEND)
    sdl2.SDL_FreeSurface(surface)
    return texture